﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyHocPhi.Entity;
using QuanLyHocPhi.DAL;
using QuanLyHocPhi.Util;

namespace QuanLyHocPhi
{
   
    public partial class frmThanhtoan : DevComponents.DotNetBar.Metro.MetroForm
    {
        float soTienYeuCau = 0;
        private int MaHoadon {get;set;}
        public frmThanhtoan(int maHd) {
            InitializeComponent();
            this.MaHoadon = maHd;
        }
        public frmThanhtoan(HOA_DON hd, float soTienYeuCau)
        {
            InitializeComponent();
            this.hd = hd;
            this.soTienYeuCau = soTienYeuCau;
            
           // this.MaHoadon = maHd;
        }
        IList<HOA_DON> dshoadon = null;
        HOA_DON_DAO hdDao = new HOA_DON_DAO();
        HOA_DON hd = new HOA_DON();
        MUC_PHI mp = new MUC_PHI();
        IList<TY_GIA> dstygia = null;
        TY_GIA tg = new TY_GIA();
        TY_GIA_DAO tgDao = new TY_GIA_DAO();
        public frmThanhtoan()
        {
            InitializeComponent();
        }
        
        private void buttonX2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmThanhtoan_Load(object sender, EventArgs e)
        {
            txtMasosv.Text = hd.SINH_VIEN.MA_SO_SV;
            txtTensv.Text = hd.SINH_VIEN.TEN_SV;
            txtTygiausd.Text = soTienYeuCau.ToString();
            cbbTendoituong.DataSource = new List<DOI_TUONG>(){ hd.MA_LOAI_LOP};
            cbbTendoituong.ValueMember = "MA_DOI_TUONG";
            cbbTendoituong.DisplayMember = "TEN_DOI_TUONG";
            if (cbbTendoituong.Items.Count > 0) cbbTendoituong.SelectedItem = 0;
            
            tg = tgDao.GetRateAtDate(hd.NGAY_KICH_HOAT);
            if (tg != null)
            {
                txtTygia.Text = tg.TY_GIA_VND.ToString();
                txtTygiavnd.Text = (tg.TY_GIA_VND * (decimal)soTienYeuCau).ToString() ;
            }
            else
            {
                MessageBox.Show("Nhập tỷ giá","Thông báo");
                btnXong.Enabled = false;
            }
        }

        private void btnXong_Click(object sender, EventArgs e)
        {
            int err = 0;
            float tmp = 0;
            float soTien = KieuDuLieu.Chuyensangsothuc(txtSotien.Text,out err);
            //kien tra
            if (soTien != soTienYeuCau)
            {
                if (MessageBox.Show("Số tiền nạp vào khác số yêu cầu, bạn có chắc muốn tiếp tục?", "Xác nhận", MessageBoxButtons.YesNo) != DialogResult.Yes)
                {
                    return;
                }
            }
            //thanh toan
            HOA_DON_CT_DAO daoHoaDonCT = new HOA_DON_CT_DAO();
            BIEN_LAI_DAO daoBienLai = new BIEN_LAI_DAO();
            IList<HOA_DON_CT> list = daoHoaDonCT.GetSubInvoicesByInvoice(hd.MA_HOA_DON);
            
            foreach (HOA_DON_CT ct in list)
            {
                if (!ct.TRANG_THAI)
                {
                    tmp += ct.SO_TIEN;
                    ct.TRANG_THAI = true;                   
                    if (tmp > soTien) break;
                    daoHoaDonCT.Update(ct);
                    
                }                
            }
            BIEN_LAI bl = new BIEN_LAI()
            {
                NGAY_THU_PHI = DateTime.Now,
                MA_HOA_DON = hd,
                MA_MUC_PHI = mp,
                MA_PHI_DT = hd.MA_LOAI_LOP,
                MA_TY_GIA = tg,
                SO_TIEN = soTien
            };
            daoBienLai.Insert(bl);
            this.Close();
        }
    }
}